package UdpTalk;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UdpReceiverDemon1 {
    public static void main(String[] args) throws Exception {
        // 1. receive port
        DatagramSocket socket=new DatagramSocket(6666);
        // 2. receive packet
        while(true){
            // prepare
            byte[] container=new byte[1024];
            DatagramPacket packet=new DatagramPacket(container,0,container.length);
            socket.receive(packet);
            // get data
            byte[] data=packet.getData();
            //data.length 会导致 receiveData 字符串中包含很多无效的空字节
            String receiveData=new String(data,0,packet.getLength());
            System.out.println(receiveData);
            // break
            if(receiveData.equals("exit")){
                break;
            }
        }
        socket.close();
    }
}
